//
//  ZDMenuView.m
//  特种车调度
//
//  Created by 陈宇 on 15/8/17.
//
//

#import "ZDMenuView.h"
#import "ZDMenuHeaderView.h"
#import "BMAccountManager.h"

@interface ZDMenuView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) ZDMenuHeaderView *headerView;

@end

@implementation ZDMenuView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self configuration];
    }
    return self;
}

- (void)configuration
{
    self.backgroundColor = [UIColor clearColor];
    
    UITableView *tableView = [[UITableView alloc] init];
    tableView.backgroundColor = BMColor(245.f, 245.f, 247.f, 1.f);
    tableView.separatorColor = BMColor(245.f, 245.f, 247.f, 1.f);
    tableView.dataSource = self;
    tableView.delegate = self;
    [self addSubview:tableView];
    self.tableView = tableView;
    ZDMenuHeaderView *headerView = [[ZDMenuHeaderView alloc] initWithFrame:CGRectZero];
    [headerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerAction)]];
    tableView.tableHeaderView = headerView;
    self.headerView = headerView;
    
    //    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240.f, 170.f)];
    BMBannerView *bannerView = [[BMBannerView alloc] initWithFrame:CGRectMake(10.f, 10.f, 220.f, 150.f)];
    [footerView addSubview:bannerView];
    tableView.tableFooterView = footerView;
    _bannerView = bannerView;
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    if (newSuperview) {
        weakSelf(self);
        User *user = [BMAccountManager sharedInstance].user;
        self.headerView.uidLabel.text = [NSString stringWithFormat:@"UID %@", kRoleType ? user.disId : user.ownId];
        self.headerView.phoneLabel.text = [NSString stringWithFormat:@"手机号 %@", user.phone];
        [weakSelf.headerView.iconView sd_setImageWithURL:[NSURL URLWithString:user.icon]];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self hide];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, 240.f, self.height);
}

- (void)show
{
    [kWindow addSubview:self];
    self.tableView.x = -240.f;
    [UIView animateWithDuration:kDefaultAnimDuration animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.25];
        self.tableView.x = 0.f;
    }];
}

- (void)hide
{
    [UIView animateWithDuration:kDefaultAnimDuration animations:^{
        self.backgroundColor = [UIColor clearColor];
        self.tableView.x = -CGRectGetWidth(_tableView.frame);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)headerAction
{
    if (_menuClicked) {
        _menuClicked(nil);
    }
    [self hide];
}

#pragma mark - UITableViewDataSource&UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _menuItems.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_menuItems[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont systemFontOfSize:14.f];
        cell.textLabel.textColor = BMColor(102.f, 102.f, 102.f, 1.f);
    }
    NSDictionary *menuItem = _menuItems[indexPath.section][indexPath.row];
    cell.textLabel.text = menuItem[@"title"];
    cell.imageView.image = [UIImage imageNamed:menuItem[@"icon"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_menuClicked) {
        _menuClicked(indexPath);
    }
    [self hide];
}

@end
